package com.drinks.yiniang.common.help;

import android.app.Activity;

import com.drinks.yiniang.R;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 快捷登录
 *
 * @author han
 * @date 2019/12/10
 */
public class QuickLoginHelp {
    private Activity mActivity;

    private IWXAPI mIWXApi;

    public QuickLoginHelp(Activity activity) {
        mActivity = activity;
    }

    public void doWxAuthLogin() {
        if (mIWXApi == null) {
            mIWXApi = WXAPIFactory.createWXAPI(mActivity, Constants.WX_APPID, true);
            mIWXApi.registerApp(Constants.WX_APPID);
        }

        if (!mIWXApi.isWXAppInstalled()) {
            ToastUtils.showShort(R.string.text_wx_is_install);
            return;
        }

        ToastUtils.showShort(R.string.text_load_wx_login);

        SendAuth.Req req = new SendAuth.Req();

        //授权作用域
        req.scope = "snsapi_userinfo";
        //用于保持请求和回调的状态
        req.state = "pugua_wx_login";

        mIWXApi.sendReq(req);
    }

    public void releaseResource() {
        mActivity = null;

        if(mIWXApi != null) {
            mIWXApi.unregisterApp();
        }
    }
}
